//职员管理

import React, {Component} from 'react';
import app from '../app';
import {Form, Input, Button, Table, Modal, Row, Upload, Icon, DatePicker, Select,Pagination} from 'antd';
import {project,employee } from '../value';
import '../css/other.css';
import moment from 'moment';
import {config} from "../config";
import {AddAccounts,EditAccounts} from '../components/operateManage';

import {EditorState, convertToRaw, ContentState} from 'draft-js';
import {Editor} from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';

const FormItem = Form.Item;
const confirm = Modal.confirm;
const RangePicker=DatePicker.RangePicker;
const Option =Select.Option;

class RechargeRecord extends Component {
    constructor(props) {
        super(props);
        this.state = {
            searchData: {},
            pagination: {current: 1, pageSize: 10, total: 0},
            data: [
                {
                    order_number:1,
                    activity_name:122,
                    activity_explain:"cbl",
                    admin_count:12,
                    start_time:'2018-12-12',
                    end_time:'2018-12-12',
                    activity_status:1,
                    created_at:'2018-12-12'
                }
            ],
            currentPageSize: 0,
            pageCount: 0,
            imgUrl: '',
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number'
        },
        {
            title: '账号',
            dataIndex: 'activity_name'
        },
        {
            title: '充值金额',
            dataIndex: 'activity_explain',
            sorter:true
        },
        {
            title: '送金额',
            dataIndex: 'admin_count',
            sorter:true
        },
        {
            title: '充值时间',
            dataIndex: 'start_time',
            render:start_time=>{
                return(
                    <span>{moment(start_time).format('YYYY-MM-DD')}</span>
                )
            }
        },
    ];

    render() {
        return (
            <div style={{paddingBottom: 25}}>
                <div className="pageTitle">
                    充值记录
                </div>
                <SearchLists refresh={()=>this.handleRefreshAfterCreate()} clickSearch={(values) => this.handleGetValue(values)}/>
                <div style={{fontSize:12,marginBottom:20,marginTop:20}}>
                    <span style={{color:'red'}}>充值总额：1000</span><span style={{color:'red',marginLeft:'20px'}}>充值送金额：1000</span>
                </div>
                <Row style={{marginTop: '20px'}}>
                    <Table
                        columns={this.columns}
                        rowKey={(record, index) => index}
                        dataSource={this.state.data}
                        loading={this.state.loading}
                        pagination={false}
                        onChange={this.handleTableChange}
                    />
                </Row>
                <Row style={{textAlign:'right',marginTop:20}}>
                    <Pagination total={50} showSizeChanger showQuickJumper />
                </Row>
            </div>
        )
    }

    /**
     * 初始化
     */
    componentDidMount() {
        //this.searchProject({});
    }

    /**
     * 获取搜索条件
     */
    handleGetValue(value) {
        const searchData = {project_name:value};
        this.setState({searchData});
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理创建后刷新事件
     */
    handleRefreshAfterCreate() {
        const param = {};
        this.searchProject(param);
    }

    /**
     * 处理删除后刷新事件
     */
    handleRefreshAfterDelete() {
        const {searchData} = this.state;
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理更新后刷新事件
     */
    handleRefreshAfterUpdate() {
        const {searchData} = this.state;
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理页码、显示数量变化事件
     * @param pagination
     * @param filters
     * @param sorter
     */
    handleTableChange = (pagination, filters, sorter) => {
        console.log(sorter);
        const current = pagination.current;
        const size = pagination.pageSize;
        const pagination1 = this.state.pagination;
        pagination1.current = current;
        pagination1.pageSize = size;
        this.setState({pagination: pagination1});
    };

    /**
     * 查找项目列表
     * @param param
     */
    searchProject(param) {
        let _this = this;
        _this.setState({loading: true});
        app.post(project.SEARCH_PROJECT, param).then(result => {
            if (result.code === 1) {
                const data=result.data;
                for (let i=0;i<data.length;i++){
                    const content=data[i].project_content;
                    if(content.length>10){
                        data[i].project_content=content.substr(0,10)+'...';
                    }
                }
                const pagination={total:data.length,current:1,pageSize:10};
                this.setState({pagination, data: data, loading: false });
            } else {
                app.alert(result.msg, 'success', 2);
                const pagination = {total: 0, current: 0, pageSize: 0};
                this.setState({pagination, data: [], loading: false});
            }
        });
    }

    /**
     * 删除项目
     * @param id
     */
    handleDeleteProject(id) {
        let _this = this;
        confirm({
            content: '您确定要删除该项目吗？',
            onOk() {
                app.post(project.DELETE_PROJECT, {project_id: id}).then((req) => {
                    if (req.code == '1') {
                        app.alert("删除成功", 'success', 2);
                        _this.handleRefreshAfterDelete();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            },
            onCancel() {
            },
        });
    }
}


class SearchList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            project_name:''
        }
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        const dateFormat='YYYY-MM-DD';
        return (
            <div style={{display: 'flex', marginTop: 10, marginBottom: 5}}>
                <div style={{display: 'flex', width: '220px', height: '28px'}}>
                    <div style={{width: 50, height: 28, lineHeight: '28px', color: '#000000'}}>账号：</div>
                    <Input onPressEnter={() => this.handleSearchComplain1()} onChange={(e) => this.handleOnChange(e)}
                           type="text" placeholder="账号" style={{width:150}}/>
                </div>
                <div style={{display: 'flex', width: '320px', height: '28px',marginLeft:10}}>
                    <div style={{width: 70, height: 28, lineHeight: '28px', color: '#000000'}}>充值时间：</div>
                    <DatePicker format={dateFormat} style={{width:220}}/>
                </div>
                <div style={{display: 'flex', width: '120px', height: '28px', marginLeft: 20}}>
                    <Button type="primary" onClick={() => this.handleSearchComplain1()}>搜索</Button>
                </div>
            </div>
        )
    };

    componentDidMount() {
    };

    /**
     * 处理账号输入框变化事件
     * @param e
     */
    handleOnChange(e) {
        this.setState({project_name: e.target.value});
    }

    /**
     * 获取搜索条件
     */
    handleSearchComplain1() {
        let {project_name} = this.state;
        this.props.clickSearch(project_name);
    };

    handleRefreshAfterCreate(){
        this.props.refresh();
    }
}

const SearchLists = Form.create()(SearchList);
const RechargeRecords = Form.create()(RechargeRecord);

export default RechargeRecords


